package com.yztc.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yztc.chunboapp.MainActivity;
import com.yztc.chunboapp.R;
import com.yztc.utils.ToastHelper;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Administrator on 2016/10/16.
 */

public class RegisterActivity extends AppCompatActivity {

    private TextView mTVRegister, mTVHongbao, mTVToLogin;
    private ImageView mClose;
    //注册和获取验证码按钮
    private Button mBtnMsg, mBtnRegister;
    //获取手机号，密码，验证码
    private EditText mETPhoneNum, mETPsd, mETMsg;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
        initView();
        toLogin();
        //点击注册
        toRegister();
    }

    private void toRegister() {
        //为注册按钮设置监听
        mBtnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取输入框的用户手机
                final String userName = mETPhoneNum.getText().toString();
                //获取输入框的密码
                final String passWord = mETPsd.getText().toString();
                //判读输入的手机号码
                final boolean blUserNamePhone = userName.matches("^[1][0-9]{10}$");
                //判断输入的密码
                final boolean blPsw = passWord.matches("^[A-Za-z0-9]{6,}$");

                if (blUserNamePhone == true && blPsw == true){
                    BmobUser bmobUser = new BmobUser();
                    bmobUser.setUsername(userName);
                    bmobUser.setPassword(passWord);
                    bmobUser.signUp(new SaveListener<BmobUser>() {
                        @Override
                        public void done(BmobUser bmobUser, BmobException e) {
                            if (e == null) {
                                ToastHelper.toast(getApplication(),"注册成功", R.mipmap.appreise_ok);
                                MainActivity.tag = true;
                                finish();
                                Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                                startActivity(intent);
                            } else {
                                int code = e.getErrorCode();
                                //202,用户名已存在
                                if (code == 202) {
                                    ToastHelper.toast(getApplication(), "用户名已注册，请更换用户名", R.mipmap.appreise_close);
                                }
                            }
                        }
                    });
                }else {
                    ToastHelper.toast(getApplication(), "注册失败：请检查用户名密码是否有误", R.mipmap.appreise_close);
                }
            }
        });
        mClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void init() {
        mETPhoneNum = (EditText) findViewById(R.id.register_userName);
        mETPsd = (EditText) findViewById(R.id.register_psw);
        mETMsg = (EditText) findViewById(R.id.register_msg);
        mBtnMsg = (Button) findViewById(R.id.register_getmsg);
        mBtnRegister = (Button) findViewById(R.id.btn_register);
        mTVRegister = (TextView) findViewById(R.id.register_xieyi);
        mTVHongbao = (TextView) findViewById(R.id.register_hongbao);
        mTVToLogin = (TextView) findViewById(R.id.register_tologin);
        mClose= (ImageView) findViewById(R.id.register_close);
    }

    private void initView() {
        mTVRegister.setText(getClickableSpan1());
        mTVHongbao.setText(getClickableSpan2());
        //此行必须有
        mTVRegister.setMovementMethod(LinkMovementMethod.getInstance());
        mTVHongbao.setMovementMethod(LinkMovementMethod.getInstance());
        //设置点击后的颜色为透明，否则会一直出现高亮
        mTVRegister.setHighlightColor(Color.TRANSPARENT);
        mTVHongbao.setHighlightColor(Color.TRANSPARENT);
    }

    private SpannableString getClickableSpan1() {
        //监听器
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, XieyiActivity.class);
                startActivity(intent);
            }
        };
        SpannableString spanableInfo = new SpannableString("注册即为同意春播注册协议");
        int start = 6;  //超链接起始位置
        int end = 12;   //超链接结束位置
        //可以为多部分设置超链接
        spanableInfo.setSpan(new Clickable(listener), start, end, Spanned.SPAN_MARK_MARK);
        spanableInfo.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorNomal)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanableInfo;
    }

    private SpannableString getClickableSpan2() {
        //监听器
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(RegisterActivity.this, "Click Success", Toast.LENGTH_SHORT).show();
            }
        };
        SpannableString spanableInfo = new SpannableString("我有红包邀请码");
        int start = 0;  //超链接起始位置
        int end = 7;   //超链接结束位置
        //可以为多部分设置超链接
        spanableInfo.setSpan(new Clickable(listener), start, end, Spanned.SPAN_MARK_MARK);
        spanableInfo.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorNomal)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spanableInfo;
    }

    //设置监听，跳转到登录页面
    private void toLogin() {
        mTVToLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    class Clickable extends ClickableSpan implements View.OnClickListener {
        private final View.OnClickListener mListener;

        public Clickable(View.OnClickListener listener) {
            mListener = listener;
        }

        @Override
        public void onClick(View view) {
            mListener.onClick(view);
        }

    }
}


